package main

import (
	"encoding/base64"
	"fmt"
	"net/mail"
	"net/smtp"
	"strings"
)

type St struct {
	Name       string
	Pwd        string
	ServerName string
	Port       string `:25`
}

func domail(to mail.Address, title string, body string) {

	auth := smtp.PlainAuth(
		"",
		conf.MailStmp.Name,
		conf.MailStmp.Pwd,
		conf.MailStmp.ServerName,
	)

	header := make(map[string]string)
	header["From"] = conf.Mailfrom.String()
	header["To"] = to.String()
	header["Subject"] = encodeRFC2047(title)
	header["MIME-Version"] = "1.0"
	header["Content-Type"] = "text/plain; charset=\"utf-8\""
	header["Content-Transfer-Encoding"] = "base64"

	message := ""
	for k, v := range header {
		message += fmt.Sprintf("%s: %s\r\n", k, v)
	}
	message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))

	err := smtp.SendMail(
		conf.MailStmp.ServerName+conf.MailStmp.Port,
		auth,
		conf.Mailfrom.Address,
		[]string{to.Address},
		[]byte(message),
	)
	checkErr(err)

}

func encodeRFC2047(String string) string {
	// use mail's rfc2047 to encode any string
	addr := mail.Address{String, ""}
	return strings.Trim(addr.String(), " <>")
}
